package com.zzq.config;


import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

@Configuration
@Slf4j
public class FabricK8sInit {

    @Autowired
    private K8sProperties k8sProperties;

    @Bean
    public KubernetesClient kubernetesClient(){
        System.out.println(k8sProperties);
        Resource resource = k8sProperties.getAdminConf();
        KubernetesClient kubernetesClient = null;
        InputStream in = null;
        Config config = null;
        try {
            in = resource.getInputStream();
            String adminConfData = IOUtils.toString(in);
            config = Config.fromKubeconfig(adminConfData);
            kubernetesClient = new DefaultKubernetesClient(config);
            return kubernetesClient;
        } catch (IOException e) {
            e.printStackTrace();
            log.error("init DefaultKubernetesClient error",e.getMessage());
        }
        return kubernetesClient;
        //return null;
    }
}
